package com.headwind.logistics.permission.service.impl;

import com.headwind.logistics.permission.mapper.MenuMapper;
import com.headwind.logistics.permission.mapper.RoleMapper;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.pojo.MenuTree;
import com.headwind.logistics.common.pojo.Role;
import com.headwind.logistics.permission.service.intl.RoleServiceIntl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-14 21:40
 * @Version 1.0
 * @DESCRIPTION 角色业务逻辑层实现类
 */
@Service
public class RoleServiceImpl implements RoleServiceIntl {
    //角色数据访问层
    @Resource
    private RoleMapper roleMapper;

    //菜单数据访问层
    @Resource
    private MenuMapper menuMapper;
    /**
     * 查询角色
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<Role> queryRoleMenu(Integer page, Integer rows) {

        if (page != null){
            page = (page-1)*rows;
        }

        return roleMapper.queryRoleMenu(page, rows);
    }

    /**
     * 根据角色id获取菜单
     * @param roleId
     * @return
     */
    @Override
    public List<Menu> queryMenuByRoleId(Long roleId) {
        return roleMapper.queryMenuByRoleId(roleId);
    }

    /**
     * 根据菜单id获取角色
     * @param menuId
     * @return
     */
    @Override
    public List<Role> queryRoleByMenuId(Long menuId) {
        return roleMapper.queryRoleByMenuId(menuId);
    }

    /**
     * 获取树形菜单
     * @return
     */
    @Override
    public List<MenuTree> queryAllRecursion(Long roleId){

        //获取角色的菜单
        List<Menu> menuList = roleMapper.queryAllMenuByRoleId(roleId);

        List<MenuTree> menuTrees = roleMapper.queryAllRecursion();


        return fun(menuList,menuTrees);
    }

    /**
     * 递归
     * @param menuList
     * @param menuTrees
     * @return
     */
    public List<MenuTree> fun(List<Menu> menuList,List<MenuTree> menuTrees){

        for (MenuTree menuTree : menuTrees) {

            for (Menu menu : menuList) {

                if (menuTree.getId() == menu.getMenuId() && menuTree.getParent_id() != 0){

                    menuTree.setChecked(true);

                }
                //递归循环
                fun(menuList,menuTree.getChildren());
            }


        }

        return menuTrees;
    }

    /**
     * 添加菜单 角色关联表
     * @param roleId
     * @param menuId
     * @return
     */
    @Override
    @Transactional
    public int addMenuRole(Long roleId, String menuId) {

        int cou = 0;
        //分割菜单id
        String[] idStr = menuId.split(",");
        //循环添加二级菜单
        for (String id : idStr) {
            //查询此角色的菜单是否已存在
            int count = menuMapper.queryMenuManagerIdByRoleId(roleId, Long.parseLong(id));
            if (count < 1){
                //添加二级菜单
                menuMapper.addMenuRole(Long.parseLong(id), roleId);
                cou++;
            }

        }


        return cou;
    }

    /**
     * 删除角色菜单关联表
     * @param roleId
     * @param menuId
     * @param menuManagerId
     * @return
     */
    @Override
    public int deleteMenuRole(Long roleId, Long menuId, Long menuManagerId) {

        int cou = 0;
        //判断此角色是否已存在二级菜单
        int i = menuMapper.queryMenuManagerIdByRoleId(roleId, menuId);
        //如果存在既删除二级菜单
        if (i > 0){
            //删除二级菜单
            menuMapper.deleteMenuRole(menuId,roleId);
            cou++;
        }
        //判断此角色是否已存在一级菜单
        /*int count = menuMapper.queryMenuManagerIdByRoleId(roleId, menuManagerId);
        //如果存在既删除一级菜单
        if (count >0){
            //删除一级菜单
            menuMapper.deleteMenuRole(menuManagerId,roleId);
            cou++;
        }*/

        return cou;
    }

    /**
     * 查询全部角色
     * @return
     */
    @Override
    public List<Role> queryAllRole() {
        return roleMapper.queryAllRole();
    }

}
